package com.swgk.core.dialog;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.swgk.core.R;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseAreaAdapter;
import com.swgk.core.recyclerview.adapter.BaseCityAdapter;
import com.swgk.core.recyclerview.adapter.BaseProvinceAdapter;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ResolutionUtil;
import com.swgk.core.util.ScaleUtils;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseAreaPopWindow<D> extends BasePopWindow {
    private LinearLayout mRecyclerLayout;// 列表高度设置
    private RecyclerView mRlProvince;//省列表控件
    private RecyclerView mRlCity;//市列表控件
    private RecyclerView mRlArea;//区列表控件
    private LinearLayoutManager mProvinceManager;
    private LinearLayoutManager mCityManager;
    private LinearLayoutManager mAreaManager;
    private LinearLayout llP;//父布局
    private BaseProvinceAdapter mProvinceAdapter;
    private BaseCityAdapter mCityAdapter;
    private BaseAreaAdapter mAreaAdapter;
    private List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list;
    private int mProvincePosition = -1;
    private int mOldProvincePosition = -1;
    private int mCityPosition = -1;
    private int mOldCityPosition = -1;
    private int mAreaPosition = -1;
    private TextView mTvReSet;
    private TextView mTvOk;
    private boolean isAllSingleSelect;
    private boolean isOnlyShowProAndCity;
    private boolean isSecondShow;
    private boolean isThreeShow;
    //是否只显示区域不限
    private boolean showOnlyAreaNoData;


    public BaseAreaPopWindow(Context context) {
        super(context);
    }

    public void BaseData(List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list) {
        this.list = list;
        mProvinceAdapter.setDatas(list);
//        reSetAdapter();
    }

    public void BaseDataAndPosition(List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list, int proPos, int cityPos, int areaPos) {
        this.list = list;
        mProvincePosition = proPos;
        mCityPosition = cityPos;
        mAreaPosition = areaPos;

        mProvinceAdapter.setDatas(list);
        mProvinceAdapter.setDefItem(proPos);
        mProvinceAdapter.setCheckDefault(proPos);
        reSetCityAndAreaAdapter();
        reSetAdapter();
        if (mCityAdapter.getDatas().size() > 0) {
            mCityAdapter.setCheckDefault(cityPos);
            mCityAdapter.setDefItem(cityPos);
        }
        if (list.get(mProvincePosition).getChildren() == null || list.get(mProvincePosition).getChildren().size() == 0) {
            return;
        }
        mAreaAdapter.setDatas(list.get(mProvincePosition).getChildren().get(mCityPosition).getChildren());
        if (mAreaAdapter.getDatas().size() > 0) {
            mAreaAdapter.setCheckDefault(areaPos);
            mAreaAdapter.setDefItem(areaPos);
        }

    }


    public void setShowOnlyAreaNoData(boolean showOnlyAreaNoData) {
        this.showOnlyAreaNoData = showOnlyAreaNoData;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_select_area;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mRecyclerLayout = holder.getPop().findViewById(R.id.recycler_layout);
        mRlProvince = holder.getPop().findViewById(R.id.rl_province);
        mRlCity = holder.getPop().findViewById(R.id.rl_city);
        mRlArea = holder.getPop().findViewById(R.id.rl_area);
        mTvReSet = holder.getPop().findViewById(R.id.tv_re_set);
        mTvOk = holder.getPop().findViewById(R.id.tv_ok);
        llP = holder.getPop().findViewById(R.id.ll_p);
        mProvinceManager = new LinearLayoutManager(mContext);
        mCityManager = new LinearLayoutManager(mContext);
        mAreaManager = new LinearLayoutManager(mContext);

        mProvinceAdapter = new BaseProvinceAdapter(mContext, null, R.layout.item_base_province);
        mCityAdapter = new BaseCityAdapter(mContext, null, R.layout.item_base_city);
        mAreaAdapter = new BaseAreaAdapter(mContext, null, R.layout.item_base_area);
        //------------------------------------------
        mRlProvince.setAdapter(mProvinceAdapter);
        mRlCity.setAdapter(mCityAdapter);
        mRlArea.setAdapter(mAreaAdapter);
        //------------------------------------------
        mRlProvince.setLayoutManager(mProvinceManager);
        mRlCity.setLayoutManager(mCityManager);
        mRlArea.setLayoutManager(mAreaManager);
        mProvinceAdapter.setDefItem(0);

        if (mProvincePosition > 0) {
            mRlProvince.scrollToPosition(mProvincePosition);
        }

        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick == null) {
                    return;
                }
                if (mProvincePosition == -1) {
                  //   MToast.showToast(mContext, "请选择城市");
                    dismiss();
                    return;
                }
                if (isAllSingleSelect) {
                    onResultClick.result(mProvinceAdapter.getDatas().get(mProvincePosition),
                            mCityAdapter.getDatas().size() > 0 && mCityPosition >= 0 ? mCityAdapter.getDatas().get(mCityPosition) : new BaseAreaEntity(),
                            mAreaAdapter.getDatas().size() > 0 && mAreaPosition >= 0 ? mAreaAdapter.getDatas().get(mAreaPosition) : new BaseAreaEntity());
                    String proName = mProvinceAdapter.getDatas().get(mProvincePosition).getName();
                    String proSimpleName = mProvinceAdapter.getDatas().get(mProvincePosition).getSimpleName();
                    if (proName.equals("不限")) {
                        proName = "全国";
                    }
                    //!TextUtils.isEmpty(proSimpleName)  处理招采返回省市时proSimpleName为空指针问题，返回数据格式和省市区的格式有点差距不一致导致的
                    if (!TextUtils.isEmpty(proSimpleName)) {
                        if (proSimpleName.equals("不限")){
                            proSimpleName = "全国";
                        }
                    }else {
                        proSimpleName = proName.substring(0,proName.length()-1);
                    }
                    String citySimpleName = "";
                    String cityName = "";
                    if (mCityAdapter.getDatas().size() != 0 && mCityPosition >= 0) {
                        cityName = mCityAdapter.getDatas().get(mCityPosition).getName().equals("不限") ? proName : mCityAdapter.getDatas().get(mCityPosition).getName();
                        //!TextUtils.isEmpty(citySimpleName)  citySimpleName，返回数据格式和省市区的格式有点差距不一致导致的
                        if (!TextUtils.isEmpty(mCityAdapter.getDatas().get(mCityPosition).getSimpleName())&&mCityAdapter.getDatas().get(mCityPosition).getSimpleName().equals("不限")) {
                            citySimpleName = proSimpleName;
                        }else {
                            citySimpleName =cityName.substring(0,cityName.length()-1);
                        }
                    }
                    String areaName = "";
                    if (mAreaAdapter.getDatas().size() != 0 && mAreaPosition >= 0) {
                        areaName = mAreaAdapter.getDatas().get(mAreaPosition).getName().equals("不限") ? cityName : mAreaAdapter.getDatas().get(mAreaPosition).getName();
                    }
                    if (!TextUtils.isEmpty(areaName) && !isOnlyShowProAndCity) {
                        resultContent(areaName, mAreaAdapter.getDatas().get(mAreaPosition).getSimpleName().equals("不限") ? citySimpleName : mAreaAdapter.getDatas().get(mAreaPosition).getSimpleName());
                    } else if (!TextUtils.isEmpty(cityName)) {
                        resultContent(cityName, citySimpleName);
                    } else {
                        resultContent(proName, proSimpleName);
                    }
                } else {
                    onResultClick.result(mProvinceAdapter.getCheckDatas(),
                            mCityAdapter.getCheckDatas(),
                            mAreaAdapter.getCheckDatas());
                }
                dismiss();
            }
        });

        mTvReSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetDataAndUI();
            }
        });
    }

    public void resetDataAndUI() {
        if (list == null || list.size() == 0) {
            return;
        }
        mProvincePosition = 0;
        if(!showOnlyAreaNoData) {
            reSetCityAndAreaAdapter();
            mCityPosition = -1;
            mAreaPosition = -1;
        }else{
            mCityPosition = 0;
            mAreaPosition = 0;
            reSetAdapter1();
        }
        reSetData();
        mProvinceAdapter.resetCheckDatas();
        mProvinceAdapter.setDefItem(0);
        mProvinceAdapter.setCheckDefault(0);
        mRlProvince.smoothScrollToPosition(0);
        provinceClick(mProvinceAdapter.getDatas().get(0), 0);
        reset();
    }

    public void setMemberHeight() {
        int height = 0;
        LinearLayoutManager layoutManager = (LinearLayoutManager) mRlProvince.getLayoutManager();
        if (layoutManager != null) {
            View viewByPosition = layoutManager.findViewByPosition(0);
            if (viewByPosition != null) {
                height = viewByPosition.getHeight();
            }
        }
        if (height <= 0) {
            height = ResolutionUtil.dp2px(mRlProvince.getContext(), 40);
        }
        mRecyclerLayout.getLayoutParams().height = (height * 7);
    }

    protected abstract void provinceClick(BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>> baseAreaEntity, int position);

    protected abstract void provinceMoreClick(List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list, int position);

    protected abstract void cityClick(BaseAreaEntity<List<BaseAreaEntity>> baseAreaEntity, int position);

    protected abstract void cityMoreClick(List<BaseAreaEntity<List<BaseAreaEntity>>> list, int position);

    protected abstract void areaClick(BaseAreaEntity baseAreaEntity, int position);

    protected abstract void areaMoreClick(List<BaseAreaEntity> list, int position);

    protected abstract void resultContent(String name, String simpleName);

    protected void reset() {

    }

    public void setShowSimpleName(boolean isShow) {
        mProvinceAdapter.setShowSimple(isShow);
        mCityAdapter.setShowSimple(isShow);
        mAreaAdapter.setShowSimple(isShow);
    }

    /**
     * 是否全都单选
     *
     * @param isSingleSelect
     */
    public void setAllSingleSelect(boolean isSingleSelect) {
        isAllSingleSelect = isSingleSelect;
    }

    /**
     * 回显选择内容只展示省份和城市
     *
     * @param onlyShowProAndCity
     */
    public void setOnlyShowProAndCity(boolean onlyShowProAndCity) {
        isOnlyShowProAndCity = onlyShowProAndCity;
    }

    /**
     * 设置省列表单选多选
     * true多选；false单选
     */
    public void setProvinceType(boolean isMore) {
        if (isMore) {
            mProvinceAdapter.setMultiSelect(true);
            mProvinceAdapter.setOnItemMultiClick((baseAreaEntities, position) -> provinceMoreClick(baseAreaEntities, position));
        } else {
            mProvinceAdapter.setItemSelect(true);
            mProvinceAdapter.setOnItemClick(new AdapterOnItemClick<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
                @Override
                public void onItemClick(BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>> listBaseAreaEntity, int position) {
                    provinceClick(listBaseAreaEntity, position);
                    if (!"".equals(listBaseAreaEntity.getNo())) {
                        if (mProvincePosition != position) {
                            mProvincePosition = position;
                            if (showOnlyAreaNoData) {
                                mCityPosition = 0;
                                mAreaPosition = 0;
                            } else {
                                mAreaPosition = -1;
                                mCityPosition = -1;
                                mAreaAdapter.setDatasMayNull(null);
                            }

                            reSetAdapter();
                            reSetData();

                        } else {
                            mOldProvincePosition = mProvincePosition;
                        }
                    } else {
                        mProvincePosition = position;
                        reSetCityAndAreaAdapter();
                        reSetData();
                    }
                }
            });
        }
    }

    /**
     * 设置市列表单选多选
     * true多选；false单选
     */
    public void setCityType(boolean isMore) {
        if (isMore) {
            mCityAdapter.setMultiSelect(true);
            mCityAdapter.setOnItemMultiClick((baseAreaEntities, position) -> {
                if (baseAreaEntities.size() > 0 && !"".equals(baseAreaEntities.get(baseAreaEntities.size() - 1).getNo())) {
                    mAreaAdapter.setDatas(baseAreaEntities.get(baseAreaEntities.size() - 1).getChildren());
                    mAreaAdapter.setDefItem(-1);

                } else {
                    mAreaAdapter.setDefItem(-1);
                    mAreaAdapter.setDatasMayNull(null);

                }
                mAreaAdapter.resetCheckDatas();
                cityMoreClick(baseAreaEntities, position);
            });
        } else {
            mCityAdapter.setItemSelect(true);
            mCityAdapter.setOnItemClick(new AdapterOnItemClick<BaseAreaEntity<List<BaseAreaEntity>>>() {
                @Override
                public void onItemClick(BaseAreaEntity<List<BaseAreaEntity>> listBaseAreaEntity, int position) {
                    cityClick(listBaseAreaEntity, position);
                    mAreaPosition = 0;
                    if (!"".equals(listBaseAreaEntity.getNo())) {
                        if (mCityPosition != position) {
                            mCityPosition = position;
                            mAreaAdapter.setDatas(list.get(mProvincePosition).getChildren().get(mCityPosition).getChildren());
                            mAreaAdapter.setDefItem(0);
                            mAreaAdapter.resetCheckDatas();
                            mAreaAdapter.setCheckDefault(0);
                        } else {
                            mOldCityPosition = mCityPosition;
                        }
                    } else {
                        mCityPosition = position;
                        mAreaAdapter.setDefItem(-1);
                        mAreaAdapter.setDatasMayNull(null);
                        mAreaAdapter.resetCheckDatas();

                    }
                }
            });
        }
    }

    /**
     * 设置区列表单选多选
     * true多选；false单选
     */

    public void setAreaType(boolean isMore) {
        if (isMore) {
            mAreaAdapter.setMultiSelect(true);
            mAreaAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<BaseAreaEntity>() {
                @Override
                public void onItemMultiClick(List<BaseAreaEntity> list, int position) {

                    if (position == 0) {
                        mAreaAdapter.resetCheckDatas();
                        mAreaAdapter.setCheckDefault(0);
                        mAreaAdapter.setDefItem(0);
                    } else {
                        if (list.size() != 0) {
                            if ("不限".equals(list.get(0).getName())) {
                                mAreaAdapter.resetCheckDatas();
                                mAreaAdapter.setCheckDefault(position);
                                mAreaAdapter.setDefItem(position);
                            }
                        }
                    }
                    areaMoreClick(list, position);
                }
            });
        } else {
            mAreaAdapter.setItemSelect(true);
            mAreaAdapter.setOnItemClick(new AdapterOnItemClick<BaseAreaEntity>() {
                @Override
                public void onItemClick(BaseAreaEntity baseAreaEntity, int position) {
                    mAreaPosition = position;
                    areaClick(baseAreaEntity, position);
                }
            });
        }
    }

    /**
     * 重新设置数据
     */
    private void reSetAdapter() {
        if (list.get(mProvincePosition).getChildren() == null) {
            return;
        }
        mCityAdapter.setDatas(list.get(mProvincePosition).getChildren());
        if (list.get(mProvincePosition).getChildren().size() == 0) {
            return;
        }
        mAreaAdapter.setDatas(list.get(mProvincePosition).getChildren().get(0).getChildren());
    }

    private void reSetAdapter1() {
        if (list.get(mProvincePosition).getChildren() == null) {
            return;
        }
        mCityAdapter.setDatas(list.get(mProvincePosition).getChildren());
        if (list.get(mProvincePosition).getChildren().size() == 0) {
            return;
        }
        mAreaAdapter.setDatas(list.get(mProvincePosition).getChildren().get(mCityPosition).getChildren());
        mAreaAdapter.setDefItem(0);
    }

    private void reSetAdapter2() {
        if (list.get(mProvincePosition).getChildren() == null) {
            return;
        }
        mCityAdapter.setDatas(list.get(mProvincePosition).getChildren());
        if (list.get(mProvincePosition).getChildren().size() == 0) {
            return;
        }
        mAreaAdapter.setDatas(list.get(mProvincePosition).getChildren().get(mCityPosition).getChildren());
        mAreaAdapter.setDefItem(mAreaPosition);
    }

    /**
     * 置空数据
     */
    private void reSetCityAndAreaAdapter() {
        mCityAdapter.setDatasMayNull(null);
        mAreaAdapter.setDatasMayNull(null);
    }

    /**
     * 状态还原 清除选择的数据
     */
    private void reSetData() {
        mCityAdapter.setDefItem(0);
        mCityAdapter.resetCheckDatas();
        if (showOnlyAreaNoData) {
            mAreaAdapter.setDefItem(0);
        } else {
            mAreaAdapter.setDefItem(-1);
        }
        mAreaAdapter.resetCheckDatas();
    }

    public void setFirstListIsGone(boolean isGone) {
        if (isGone) {
            mRlProvince.setVisibility(View.GONE);
        } else {
            mRlProvince.setVisibility(View.VISIBLE);
        }
    }

    public void setSecondaryListIsGone(boolean isGone) {
        this.isSecondShow = isGone;
        if (isGone) {
            mRlCity.setVisibility(View.GONE);
        } else {
            mRlCity.setVisibility(View.VISIBLE);
        }
        setOneListStyle();
    }

    public void setThreeListIsGone(boolean isGone) {
        this.isThreeShow = isGone;
        if (isGone) {
            mRlArea.setVisibility(View.GONE);
        } else {
            mRlArea.setVisibility(View.VISIBLE);
        }
        setOneListStyle();
    }

    /**
     * 显示√
     */
    private void setOneListStyle() {
        if (isThreeShow && isSecondShow) {
            mProvinceAdapter.setImgStyle();
        }
    }

    /**
     * 设置父布局边距
     *
     * @param left
     * @param right
     */
    public void setLayoutPadding(int left, int right) {
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) llP.getLayoutParams();
        lp.leftMargin = ScaleUtils.px2dip(mContext, left);
        lp.rightMargin = ScaleUtils.px2dip(mContext, right);
        llP.setLayoutParams(lp);
    }

    public interface IProvince {
        void provinceClick(BaseAreaEntity baseAreaEntity, int position);
    }

    public void setDefaultByAreaCode(int position) {
        mProvinceAdapter.setDefItem(position);
        mRlProvince.scrollToPosition(position);
    }

    public void setCityDefaultByAreaCode(int position) {
        mCityAdapter.setDefItem(position);
        mRlCity.scrollToPosition(position);
    }

    public void setDefaultForPosition(int position) {
        mProvincePosition = position;
        mCityPosition = 0;
        mAreaPosition = -1;
        reSetAdapter();
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
        mRlProvince.scrollToPosition(position);
        mCityAdapter.setDefItem(mCityPosition);
//        reSetData();
    }

    public void setDefaultForPosition(int position, int cityPosition) {
        mProvincePosition = position;
        mCityPosition = cityPosition;
        mAreaPosition = 0;
        reSetAdapter1();
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
        mRlProvince.scrollToPosition(position);
        mCityAdapter.setDefItem(mCityPosition);
//        reSetData();
    }

    public void setDefaultForPosition(int position, int cityPosition, int areaPosition) {
        mProvincePosition = position;
        mCityPosition = cityPosition;
        mAreaPosition = areaPosition;
        reSetAdapter2();
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
        mRlProvince.scrollToPosition(position);
        mCityAdapter.setDefItem(mCityPosition);
        mAreaAdapter.setDefItem(mAreaPosition);
//        reSetData();
    }

    public void setDefPosition(int mProvincePosition, int mCityPosition) {
        this.mProvincePosition = mProvincePosition;
        this.mCityPosition = mCityPosition;
        if (list == null || list.size() == 0) {
            return;
        }
        setDefaultByAreaCode(mProvincePosition);
        if (mCityPosition == 0) {
            mCityAdapter.setDatas(new ArrayList<>());
        } else {
            mCityAdapter.setDatas(list.get(mProvincePosition).getChildren());
        }
        mCityAdapter.setDefItem(mCityPosition);
    }

    public void setDefPosition(int mProvincePosition, int mCityPosition, int mAreaPosition) {
        this.mProvincePosition = mProvincePosition;
        this.mCityPosition = mCityPosition;
        this.mAreaPosition = mAreaPosition;
        if (list == null || list.size() == 0) {
            return;
        }
        setDefaultByAreaCode(mProvincePosition);
        if (!isSecondShow) {
            if (mCityPosition == 0) {
                mCityAdapter.setDatas(new ArrayList<>());
            } else {
                mCityAdapter.setDatas(list.get(mProvincePosition).getChildren());
            }
            mCityAdapter.setDefItem(mCityPosition);
            mRlCity.scrollToPosition(mCityPosition);
        }
        if (!isThreeShow) {
            if (mAreaPosition == 0) {
                mAreaAdapter.setDatas(new ArrayList<>());
            } else {
                mAreaAdapter.setDatas(list.get(mProvincePosition).getChildren().get(mCityPosition).getChildren());
            }
            mAreaAdapter.setDefItem(mAreaPosition);
            mRlArea.scrollToPosition(mAreaPosition);
        }
    }

    public int getProvincePosition() {
        return mProvincePosition;
    }

    public int getmCityPosition() {
        return mCityPosition;
    }

    public int getmAreaPosition() {
        return mAreaPosition;
    }
}
